import storage from 'good-storage';
import delCookie from './cookie'
// 定义一个key代表存储搜索历史：
const SEARCH_KEY = "__search__"
// 定义一个key代表播放器相关设置
const PLAYER_KEY = "__player__"
// 定义一个key代表最近播放的歌曲,最大长度200
const PLAY_KEY = "__play__"
const PLAY_MAX_LEN = 200
// 定义一个key代表用户点击过最喜欢的歌曲
const FAVORITE_KEY = '__favorite__'
const FAVORITE_MAX_LEN = 200
// 定义一个key代表用户是否登录状态：
const LOGIN_STATE_KEY = '__loginstate__'
// 定义一个key存储用户信息：
const USER_INFO_KEY = '__userinfo__'
// 定义一个key存储用户喜欢的音乐(长列表节流):
const USER_LIKE_MUSIC = '__userlikemusic__'
// 只缓存十五条数据
const SEARCH_MAX_LEN = 15


// arr总数组，val插入的数组，maxlen总条数
function insertArray(arr, val, maxLen) {
  const index = _findIndex(arr, val);
  // 如果搜到第一条数据返回即可
  if (index === 0) {
    return
  }
  // 如果数组中有这条数据，还不是在最前面，则删除之前的数据
  if (index > 0) {
    arr.splice(index, 1);
    console.log('删除已存在的数据')
  }
  // 把当前的插入最前面
  arr.unshift(val)
  // 当存储的长度大于设置的长度则移除最后一条：
  if (maxLen && arr.length > maxLen) {
    arr.pop()
  }
}
export function saveSearch(query) {
  // 获取当前,如果没有存过则是空数组：
  let searches = storage.get(SEARCH_KEY, [])
  insertArray(searches, query, SEARCH_MAX_LEN);
  storage.set(SEARCH_KEY, searches);
  return searches
}

// 遍历数组对象，找到插入值的索引(这个方法其实不太好，其实就是把对象转换成字符串后向后不断对比，应该根据id来判断是否一致)
function _findIndex(l, o) {
  var objStr = JSON.stringify(o)
  return l.reduce((index, ele, i) => {
    if (JSON.stringify(ele) === objStr) {
      return i
    } else {
      return index
    }
  }, -1)
}
// 从本地缓存读取searchlist
export function loadSearch() {
  return storage.get(SEARCH_KEY, [])
}
// 从数组删除方法：
function deleteFromArray(arr, val) {
  const index = _findIndex(arr, val);
  if (index > -1) {
    arr.splice(index, 1)
  }
}

// 删除搜索历史的某一项：
export function deleteSearch(query) {
  let searches = storage.get(SEARCH_KEY, []);
  deleteFromArray(searches, query);
  // 删除完再保存数组：
  storage.set(SEARCH_KEY, searches);
  // 同时返回被删除的数组
  return searches
}


export function clearSearch() {
  storage.remove(SEARCH_KEY)
  return []
}

/**
 * 播放器默认设置
 */

export function saveOptions(obj) {
  storage.set(PLAYER_KEY, obj);
  return obj
}

export function loadOptions() {
  return storage.get(PLAYER_KEY, {
    volume: 0.5
  })
}


// 存储当前歌曲到历史记录
function insertSong(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare)
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val)
  if (maxLen && arr.length > maxLen) {
    arr.pop()
  }
}

export function savePlay(song) {
  let songs = storage.get(PLAY_KEY, [])
  insertSong(songs, song, (item) => {
    return item.id === song.id
  }, SEARCH_MAX_LEN);
  storage.set(PLAY_KEY, songs)
  return songs
}

export function loadPlay() {
  return storage.get(PLAY_KEY, [])
}



export function saveFavorite(song) {
  let songs = storage.get(FAVORITE_KEY, []);
  insertSong(songs, song, (item) => {
    return song.id === item.id
  }, FAVORITE_MAX_LEN)
  storage.set(FAVORITE_KEY, songs)
  return songs
}

export function deleteFavorite(song) {
  let songs = storage.get(FAVORITE_KEY, []);
  deleteFromArray(songs, song)
  storage.set(FAVORITE_KEY, songs)
  return songs
}

export function loadFavorite() {
  return storage.get(FAVORITE_KEY, [])
}

//删除用户喜欢的音乐
export function removefavoriteAll() {
  storage.remove(FAVORITE_KEY);
  return []
}




// 用户信息：



function delArrVal(arr, val) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == val) {
      arr.splice(i, 1)
      i--;
    }
  }
  return arr;
}
// 删除用户喜欢的音乐idlist中的某一项
// export function deleteLikeIdListCache(songid){
//   let songs = storage.get(USET_LIKE_MUSIC_IDLIST, []);
//   delArrVal(songs, songid)
//   storage.set(USET_LIKE_MUSIC_IDLIST, songs)
//   return songs 
// }

// 保存登录信息：
export function saveUserInfo(info) {
  storage.set(USER_INFO_KEY, info);
  return info
}
// 读取用户信息：
export function loadUser() {
  return storage.get(USER_INFO_KEY, {})
}











// 保存用户喜欢的音乐到缓存：
export function savaLikeMusic(list) {
  storage.set(USER_LIKE_MUSIC, list);
  return list
}
// 读取用户喜欢的歌曲：
export function loadLikeMusic() {
  return storage.get(USER_LIKE_MUSIC, [])
}


export function saveLikeSong(song) {
  let songs = storage.get(USER_LIKE_MUSIC, []);
  insertSong(songs, song, (item) => {
    return song.id === item.id
  })
  storage.set(USER_LIKE_MUSIC, songs)
  return songs
}

export function deleteLikeSong(song) {
  let songs = storage.get(USER_LIKE_MUSIC, []);
  deleteFromArray(songs, song)
  storage.set(USER_LIKE_MUSIC, songs)
  return songs
}

export function loadLikeSong() {
  return storage.get(USER_LIKE_MUSIC, [])
}

//删除用户喜欢的音乐
export function removeLikeAll() {
  storage.remove(USER_LIKE_MUSIC);
  return []
}






// 清空用户登录信息:
export function clearUserInfo() {
  storage.remove(USER_INFO_KEY);
  storage.remove(USER_LIKE_MUSIC);

  return {}
}
